const _function = require("../../utils/functionData.js")
Page({
  data: {
    lectureList: [],
    lecture: {},
    lectureId: '',
    isJoin: 0,
    status: 0
  },
  // onLoad: function (options) {
  //   // 这里假设通过 options.id  获取讲座 ID，然后根据 ID 从后端获取讲座详情
  //   // 为了示例，我们模拟一个讲座数据
  //   // const mockLecture = {
  //   //   id: 1,
  //   //   name: "微信小程序开发讲座",
  //   //   startTime: "2024-10-01 14:00",
  //   //   participantsNumber: 100,
  //   //   applicantsNumber: 50
  //   // };
  //   // this.setData({ 
  //   //   lecture: mockLecture
  //   // });
  // },
  onLoad(options) {
    this.setData({
      lectureId: options.id,
      status: options.status
    })

    this.getLectureList()
  },

  getLectureDetail(e) {
    console.log('选择时间', getApp().globalData.userInfo);
    const param = {
    }
    _function.getLectureDetail(this.data.lectureId, param, this.getLectureDetailtBack, this)
  },
  getLectureDetailtBack(res) {
    let that = this
    let dataAll = res.data
    if (dataAll.code == 200) {

      that.data.lectureList.forEach((item) => {
        console.log('视频列表', dataAll);
        if (item.id == dataAll.data.id) {

          that.setData({
            isJoin: 1
          })
        }
      })
      that.setData({
        lecture: dataAll.data
      })
      console.log('lecture', this.data.lecture, this.data.isJoin);
      wx.showToast({
        title: dataAll.msg,
        icon: 'none', // 不显示图标
        duration: 1500, // 提示的延迟时间，单位毫秒，默认为 1500
        mask: true, // 是否显示透明蒙层，防止触摸穿透，默认为 `false`
        success: () => {
          // console.log('登录失败提示已显示');
        }
      });
    }
  },
  getLectureList(e) {
    console.log('选择时间', getApp().globalData.userInfo);
    const param = {
      studentId: getApp().globalData.userInfo.userId
    }
    _function.getLectureList(param, this.getLectureListBack, this)
  },
  getLectureListBack(res) {
    let dataAll = res.data
    if (dataAll.code == 200) {
      console.log('视频列表', dataAll);
      this.setData({
        lectureList: dataAll.rows
      })
      this.getLectureDetail()
      wx.showToast({
        title: dataAll.msg,
        icon: 'none', // 不显示图标
        duration: 1500, // 提示的延迟时间，单位毫秒，默认为 1500
        mask: true, // 是否显示透明蒙层，防止触摸穿透，默认为 `false`
        success: () => {
          // console.log('登录失败提示已显示');
        }
      });
    }
  },
  handleEnroll: function () {
    // 处理报名逻辑，这里可以调用后端接口进行报名操作
    console.log(' 用户点击了报名按钮');
    const param = {
      lectureId: this.data.lectureId,
      studentId: getApp().globalData.userInfo.userId
    }
    console.log('lecture.isEnd',this.data.status,this.data.lecture.isEnd);
    if (this.data.status == 0 && this.data.lecture.isEnd == 0) {
     
      _function.postLectureBooking(param, this.postLectureBookingBack, this)
    }
  },

  postLectureBookingBack(res) {
    let that = this
    let dataAll = res.data
    if (dataAll.code == 200) {

      that.data.lectureList.forEach((item) => {
        console.log('视频列表', dataAll);
        // if (item.id == dataAll.data.id) {

        //   that.setData({
        //     isJoin: 1
        //   })
        // }
      })
      that.setData({
        lecture: dataAll.data
      })
      console.log('lecture', this.data.lecture, this.data.isJoin);
      wx.showToast({
        title: '报名成功',
        icon: 'none', // 不显示图标
        duration: 1500, // 提示的延迟时间，单位毫秒，默认为 1500
        mask: true, // 是否显示透明蒙层，防止触摸穿透，默认为 `false`
        success: () => {
          // console.log('登录失败提示已显示');
        }
      });
      wx.navigateBack()
    }
  },
})